home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / LIB211.ZIP;1 / DHUNG2.TXT < prev    next >
Encoding:
Text File  |  1993-12-14  |  19.2 KB  |  486 lines

  1.                                 dUFLP/dHUNG
  2.                      Coding Standards and Conventions
  3.                                 Version 0.8
  4.                                July 23, 1993
  5.  
  6.  
  7. dUFLP -- dBASE Users Function Library Project -- Coding Standards and
  8. Conventions -- Based upon the HUNGARIAN / xBase Conventions by Robert A.
  9. DiFalco. These Standards and Conventions have been defined and generally
  10. agreed upon by several of the 'regulars' on the Borland (once Ashton-
  11. Tate) Bulletin Board System (BORBBS (now defunct)), but are by no means 
  12. a rigid definition.
  13.  
  14. This file contains modifications to Robert DiFalco's (CIS: 71610,1705)
  15. HUNG standard for X-Base languages; it was developed by members of the
  16. Ashton-Tate/Borland Bulletin Board System to be used in a user-developed
  17. library.  Several aspects of this document have been changed, as the 
  18. dUFLP standards are more aimed at dBase than other programming 
  19. languages, but have been only slightly modified where needed, and some 
  20. duplicate definitions were removed in the variable naming practices. The
  21. original document (HUNG14.ZIP) may be found on the CompuServe dBASE 
  22. Forum.
  23.  
  24. The purpose of this document is to define some coding standards to be 
  25. used in the library for dUFLP, Please attempt to follow these standards 
  26. as closely as possible when creating Functions and Procedures to be 
  27. included in this library. Thank you ... (most of the text from this 
  28. point on is DiFalco's own words)
  29. ------------------------------------------------------------------------
  30. Let me start off by stating that these are only my opinions on naming
  31. conventions for xBase programming. In my opinion these methods are
  32. extensible to C, ASM, BASIC and just about any other programming
  33. language. It looks very similar to Hungarian notation as outlined by
  34. Charles Simonyi of Microsoft Corporation. Don't be fooled, however, this
  35. is much different. I find it more intuitive and logical though that may
  36. pertain only to my thinking.
  37.  
  38. These conventions take in the same factors as outlined by Charles 
  39. Simonyi for creating names in a program. The factors listed below are 
  40. directly quoted from his monograph.
  41.  
  42. Begin quote:
  43. 1. Mnemonic value   - so that the programmer can remember the name.
  44.  
  45. 2. Suggestive value - so that others can read the code easily.
  46.  
  47. 3. "Consistency"    - this is often viewed as an aesthetic idea, yet it
  48.                       also has to do with the information efficiency of 
  49.                       the program text. Roughly speaking, we want 
  50.                       similar names for similar quantities.
  51.  
  52. 4. Speed of decision- we cannot spend too much time pondering the name 
  53.                       of a single quantity, nor is there time for typing
  54.                       and editing extremely long variable names.
  55. End quote:
  56.  
  57. In dBase, our variable names have even a more pressing reason for short
  58. identifiers since we only have 10 characters to work with. To alleviate
  59. using needless characters, we will use lowerUpper combinations instead 
  60. of dividing underscores.
  61.  
  62. =====================================
  63. Procedure/Function Naming conventions
  64. =====================================
  65. Procedures and Functions will be heretofore called "Functions". Function
  66. names will not be "typed" as variables. As dBase in general heads 
  67. towards object oriented approaches it is important to allow our 
  68. Functions to return multiple types. This negates the approach of typing
  69. Functions by return variable type. Instead we will apply a "loose" set 
  70. of rules to Functions. Native language functions will be in all 
  71. lowercase while 3rd party or "home grown" functions (UDF or User-
  72. Defined Functions) will be mixed case beginning with a single Capital
  73. letter.
  74.  
  75. -------------
  76. UDF Name Case
  77. -------------
  78. All User Defined Functions will be mixed case, with no underscores. The
  79. first letter will be upper case, e.g. AllTrim.
  80.  
  81. 1. Functions will start with a capital letter followed by lower case
  82.    letters thus distinguishing them from variables. If the Function name
  83.    can be better expressed by two or three identifiers an UpperLower
  84.    combination will be used rather than underscores to delimit 
  85.    identifiers. Consider the following Examples:
  86.  
  87.    ClrSet()    - Sets colors
  88.    SaveGets()  - Saves active get list
  89.    PrnScr()    - Prints the screen
  90.    GetPass()   - Gets a password
  91.    Choose()    - Menu of items to "choose" from
  92.  
  93. 2. Conversion Functions will start with the value they take and end with
  94.    converted value separated by the number 2. Some examples would be as
  95.    follows:
  96.  
  97.    Str2Arr()  - Changes a string to an array.
  98.    Hex2Dec()  - Changes a Hex string into a decimal numeric.
  99.    Clr2Attr() - Changes a color string to an integer attribute.
  100.    Dbf2Arr()  - Loads a DBF into an array.
  101.  
  102. 3. Where possible, use standard qualifiers as outlined in the section
  103.    dedicated to "Variable Naming Conventions". A few examples follow.
  104.  
  105.    cFName   = HCUST->FNAME
  106.    cLName   = HCUST->LNAME
  107.    cAddr    = HCUST->ADDR
  108.    nAge     = HCUST->AGE
  109.    lActive  = HCUST->ACTIVE
  110.    nBalance = HLOAN->BALANCE
  111.    ( Please note that the storage operators ( "=" ) are lined up for 
  112.      easy reading giving a table appearance. )
  113.  
  114. 4. Where possible, express the function or procedure in less than three
  115.    qualifiers ( names ). Refer to the examples given for Rule 1.
  116.  
  117. 5. The keywords PROCEDURE, FUNCTION and RETURN shall be in all 
  118.    uppercase.
  119.  
  120.    FUNCTION SayStr
  121.  
  122.       parameters cMsg
  123.  
  124.       m->xSavClr = set( "ATTRIBUTES" )
  125.       set color to rg+/r
  126.  
  127.       ? m->cMsg
  128.  
  129.       set color to &xSavClr.
  130.  
  131.    RETURN ""
  132.  
  133. dBASE native functions shall be typed in all lowercase to delineate them
  134. from non-native functions as in the above call to set(). 
  135.  
  136.  
  137. ------------------
  138. Reserved Word Case
  139. ------------------
  140. Except for the reserved words 'FUNCTION', 'PROCEDURE', and 'RETURN', all
  141. dBASE reserved words should be lower case.
  142.  
  143. -----------------------------
  144. File Name and Field Name Case
  145. -----------------------------
  146. All reference to DOS files (e.g. .dbf, .fmt, .frg, etc.) and field
  147. names are to be in upper case.  Underscores are allowed.
  148.  
  149. ------------
  150. Case Summary
  151. ------------
  152. all lower                 : built-in commands and functions
  153. mIxed, fIrst lEtTer lOWer : memvars
  154. MiXed, First LetTer UpPer : UDFs
  155. ALL UPPER                 : files and fields, and the commands
  156.                             FUNCTION, PROCEDURE, and RETURN
  157.  
  158. =============================================
  159. DBF and Field related Punctuation Conventions
  160. =============================================
  161. There are some rules noteworthy for DBF, NDX/MDX, ALIAS and FIELD
  162. operations to delineate them from Functions and Variables. FIELD names 
  163. will NOT be typed by a lowercase type identifier as with Variables.
  164.  
  165. 1. Database and Index files, as well as Field names will always be
  166.    expressed in capital letters. As dBase's main point of existence is 
  167.    the manipulation of DataFiles, this will make them stand out against 
  168.    variables and other qualifiers.
  169. 2. Where possible they will use the same standard qualifiers used for
  170.    Variables and Functions. See the examples for Rule 3 of
  171.    "Procedure/Function Naming Conventions".
  172.  
  173. 3. Fields will be referenced by an ALIAS. Consider the following.
  174.  
  175.    HACCT->ACCNUM
  176.    HCONST->PASSWORD
  177.    HCONST->COMPANY
  178.  
  179. 4. Variables referencing Fields will have the same name as the Field 
  180.    with the addition of the dHUNG type prefix.
  181.  
  182.    cFName   = HCUST->FNAME
  183.    cLName   = HCUST->LNAME
  184.    cAddr    = HCUST->ADDR
  185.    nAge     = HCUST->AGE
  186.    lActive  = HCUST->ACTIVE
  187.    nBalance = HLOAN->BALANCE
  188.  
  189.    ( Please note that the storage operators ( "=" ) are lined up for 
  190.      easy reading giving a table appearance. )
  191.  
  192.    Variables should use the prefix: 'M->' to indicate to dBASE that
  193.    they are memory variables. This will speed the internal processing
  194.    of any routine, as dBASE always checks the list of fieldnames before
  195.    making changes to, or referring to memory variables. By using the
  196.    prefix 'M->', dBASE knows it is a memory variable, and bypasses
  197.    the list of fieldnames.
  198.  
  199. 5. With regard to DBF file names, data, index or otherwise they should
  200.    all begin with a common prefix and that prefix is similar to the
  201.    name.
  202.  
  203.    This insures that I have few naming conflicts with other systems
  204.    that may reside on the computer and just about guarantees I can
  205.    separate my files out if they get placed into a common subdirectory.
  206.  
  207. 6. Index filenames should reflect the file that they belong to but
  208.    should not attempt to indicate what the index expression is.  A
  209.    much cleaner routine results when index files are simply numbered.
  210.  
  211.    HACCTS.DBF ---> HACCTS1.NDX
  212.                    HACCTS2.NDX
  213.                    HACCTS3.NDX
  214.  
  215.    Internally the only information I am required to keep is the index 
  216.    key in an array and all indexes can be rebuilt in the order of their
  217.    respective suffix number.
  218.  
  219. ===============================
  220. Command Punctuation Conventions
  221. ===============================
  222. This area of my dHUNG naming conventions will probably meet with the 
  223. most opposition. User Defined Commands will use descriptive names as 
  224. outlined in "Procedure/Function Naming Conventions".  User Defined or 
  225. not, Commands will have one Rule.
  226.  
  227. 1. All Commands will be typed with lower case letters.
  228.  
  229. The reasons for this are simple. This will delineate commands from
  230. variables, functions and DBF elements. Consider the following examples.
  231.  
  232. FUNCTION CmdExample
  233.  
  234.    use HCUST 
  235.    set index to HCUST1, HCUST2, HCUST3
  236.  
  237.    cLName = "DiFalco"
  238.  
  239.    seek m->cLName
  240.  
  241.    HCUST->AGE    = 28
  242.    HCUST->ACTIVE = .t.
  243.    HCUST->LNAME  = m->cLName
  244.  
  245.    close database
  246.  
  247. RETURN ""
  248.  
  249. ( Please take note that in replace statements we also line up the "with"
  250.   portion of the command as we do storage operators. )
  251.  
  252. ===========================
  253. Variable Naming Conventions
  254. ===========================
  255. This is the heart of a well designed system. Variable names must give 
  256. the most amount of information possible in its name while using the 
  257. minimum number of characters possible.
  258.  
  259. All memory variables (memvars) are mixed case names, consisting of 1-10
  260. alphanumeric characters.  The first character is always a lower case
  261. type prefix.  Underscores are only used as scope prefixes.  Memvar names
  262. should not repeat type prefix information.
  263.  
  264. But memvars may have a combination of tags that will in most cases 
  265. appear in this order.
  266.  
  267. 1. A single lower case variable defining its type as returned by a 
  268.    Type() function called a "Type Prefix".
  269. 2. An Optional state called a "State Qualifier"
  270. 3. A "Standard Qualifier Tag".
  271. 4. An Optional "Pointer Reference".
  272.  
  273. -------------
  274. Type Prefixes
  275. -------------
  276. A type prefix is the first character of a variable that represents the
  277. type the programmer intends for the variable.  Below is the list of
  278. dBASE-specific type prefixes together with examples:
  279.  
  280.              Prefix          Type          Example
  281.              ------          ----          -------
  282.                 a            Array         aList
  283.                 c            Character     cFName
  284.                 d            Date          dPmtDue
  285.                 f            Float         fAngle
  286.                 l            Logical       lExitMenu
  287.                 m            Menu          mMain
  288.                 n            Numeric       nHours
  289.                 p            Pad           pOpenDbf
  290.                 s            Screen        sBackgrnd
  291.                 u            popUp         uFlds
  292.                 w            Window        wError
  293.                 x            undefined     xLookUp
  294.  
  295. For temporary variables of distinct type or pointers this single prefix 
  296. can be used by itself. Consider this example.
  297.  
  298. m->n = 0
  299. do while m->n < m->nFldMax
  300.    m->n = m->n + 1
  301.    aFldName[n] = Field( m->n )
  302. enddo
  303.  
  304. --------------
  305. Scope Prefixes
  306. --------------
  307. In dBASE, a variable can be either local to a procedure or global to all
  308. procedures.  This distinction is represented by the scope prefix.
  309. Variables that are global are represented by an underscore after the
  310. type prefix; variables that are local to a procedure have a null scope
  311. prefix.  For example, c_DbfFile is a character variable that is global;
  312. cDbfFile would be the same variable if it was local.
  313.  
  314. -------------------------
  315. Sample "State Qualifiers"
  316. -------------------------
  317. New  - a New state
  318. Sav  - a Saved state
  319. Tem  - a Temporary state
  320.  
  321. --------------------------------
  322. Sample "Standard Qualifier" tags
  323. --------------------------------
  324. Attr - Attribute
  325. Clr  - Color
  326. Crs  - Cursor
  327. Dbf  - of or pertaining to a DBF
  328. F    - First as in cFName
  329. File - Any type of file
  330. Fld  - Field
  331. L    - Last as in cLName
  332. Msg  - Message
  333. Name - a name
  334. Ndx  - of or pertaining to an Index ( or Mdx )
  335. Rec  - Record Number
  336. Ret  - Return value ( lRet = .f. )
  337. Str  - String
  338. T    - Top
  339. L    - Left
  340. B    - Bottom
  341. R    - Right
  342. Row  - Row
  343. Col  - Column
  344. X    - Row
  345. Y    - Column
  346.  
  347. Please note that Standard Qualifiers can be used in combinations as in 
  348. the following examples.
  349.  
  350.    nTRow    - Top Row
  351.    cFName   - First name
  352.    cDbfFile - a Database file
  353.    cNdxFile - an Index File
  354.  
  355. ---------------------------
  356. Sample "Pointer References"
  357. ---------------------------
  358. 1,2,3 - State pointer references as in cSavClr1, cSavClr2, etc.
  359. Max   - Strict upper limit as in nFldMax, maximum number of Fields
  360. Min   - Strict lower limit as in nRecMin, minimum number of Records
  361.  
  362.  
  363. These lists are to serve as samples and building blocks. They can and
  364. should be added to. Lets look at a few examples of the conventions at 
  365. work. This should dispel the myth that notation conventions cannot be 
  366. applied to variables with a 10 character maximum length.
  367.  
  368. ==========================================
  369. Poorly Designed and Well Designed Examples
  370. ==========================================
  371.  
  372. WRONG       RIGHT       WHY
  373. ------------------------------------------------------------------------
  374. nFldNum     nFld        Number is indicated by the Type Prefix making 
  375.                         the word Num redundant and a needless use of 
  376.                         character space.
  377. Count       n           n serves as a temporary count index. Count has 
  378.                         no Type Prefix - could also be called nCount.
  379. Last_Name   cLName      This is one of the most horrendous mistakes. 
  380.                         First we do not specify the variable type. 
  381.                         Second we needlessly spell out LAST and worst of
  382.                         all we use up a precious character by using an 
  383.                         underscore instead of using the UpperLower 
  384.                         combination.
  385. SaveScreenA sSav1       No Type Prefix, needless use of characters. The
  386. SaveScreenB sSav2       alphabetical reference is allowable though I 
  387. SaveScreenC sSav3       prefer to use a numeric reference. Use of 's' 
  388.                         type prefix removes need to use Scrn or sScreen.
  389. ColorStr    xClr        No Type Prefix. Needless use of characters. This 
  390.                         shows the usage of 'unknown' type with the Type 
  391.                         Prefix 'x' (colors are character strings in 
  392.                         specific formats, but do not require a special 
  393.                         type).
  394. nRecNo      nRec        The use of "No" is redundant.
  395. PrintReset  xPrnReset   Also could have been shortened to cPrnRst.
  396. MessageStr  cMsg        These are starting to become obvious, don't you
  397.                         think?
  398.  
  399. ======================
  400. Standard Header Format
  401. ======================
  402. Below is the recommended header format to be used for procedures/
  403. functions. This will allow the automated maintenance of library 
  404. functions as well as documenting the routine.  By using this header, it
  405. will make it possible to create a routine either in dBASE or some other
  406. software that can generate a library listing, giving the pertinent 
  407. information. Please also note EoF/EoP comment at the END of the 
  408. function or procedure. In order to speed up processing, we are using 
  409. the prefix "M->" to specify memory variables in the code. Finally, note
  410. that we are keeping text in particular, although it is useful to keep 
  411. the code in the same limit, to a right margin of 72 characters. 
  412.  
  413. FUNCTION AllTrim && {ver 0.8}
  414. *-----------------------------------------------------------------------
  415. *-- Programmer..: HazMatZak
  416. *-- Date........: 07/02/1991
  417. *-- Notes.......: Used to remove leading and trailing blanks from
  418. *--               a string.
  419. *-- Written for.: dBASE IV, 1.1
  420. *-- Rev. History: 05/23/1991 0.1 - Original version (CLW)
  421. *--               06/18/1991 0.2 - Revision to modify for dHUNG
  422. *--               06/19/1991 0.3 - Revised for ...
  423. *--               06/24/1991 0.4 - Revised for ...
  424. *--               06/25/1991 0.5 - Revised again by Ken Mayer
  425. *--                                to give smaller header ...
  426. *--               etc.
  427. *-- Calls.......: None
  428. *-- Called by...: Any
  429. *-- Usage.......: AllTrim(<cArg>)
  430. *-- Example.....: ? AllTrim("  Test string  ")
  431. *-- Returns.....: String to be trimmed (i.e.:"Test string")
  432. *-- Parameters..: cArg = String to be trimmed
  433. *-----------------------------------------------------------------------
  434.        
  435.    parameters cArg
  436.  
  437. RETURN ltrim(rtrim(m->cArg))
  438. *-- EoF: AllTrim()
  439.  
  440. -------------
  441. Documentation
  442. -------------
  443. It is always a good idea to document code -- never assume you will be 
  444. the only programmer to read your code. If it is being posted on 
  445. CompuServe, a lot of people may download it. If they wish to understand 
  446. what you are doing, or use just portions of your code, it is very useful
  447. to explain in at least some detail what your program/function/procedure
  448. is doing. We are not going to suggest more than attempt to make the 
  449. documentation neat, concise, and to the point (don't ramble).
  450.  
  451. -----------
  452. dUFLP NOTES
  453. -----------
  454. Thinking of contributing to the dBASE Users' Function Library Project?
  455. If so, please consider the following:
  456.  
  457. 1) Margins 72 characteres wide (both documentation _and_ code).
  458. 2) Use of "m->" prefix.
  459. 3) Use of the dot at the end of macro substitution calls (¯o.).
  460. 4) Use spaces, rather than tabs, and please try to indent the code to
  461.    three spaces (for each indent), rather than 8, which is the dBASE
  462.    default tab.
  463. 5) Please use the header as shown above for all code (exactly as it
  464.    is above, down to the number of dots in the header lines).
  465. 6) Please use the "private" statement for all memvars used within your
  466.    function or procedure. 
  467.  
  468.    Also, please read the file: CONTRIB.TXT.
  469.  
  470. ----------------------
  471. dHUNG Revision History
  472. ----------------------
  473. 1991-05-23  0.1  Initial revision (clw)
  474. 1991-06-18  0.2  Revision after notations vote (clw)
  475. 1991-06-19  0.3  Memvars, Files, Fields, UDFs, Case Summary (HazMatZak)
  476. 1991-08-20  0.4  Added official header example (clw)
  477. 1991-08-26  0.5  Combined original DiFalco document (HUNG14.ZIP) with 
  478.                  CLW's dHUNG documentation, and the header information
  479.                  (KenMayer).
  480. 1991-08-30  0.6  Minor corrections to make this dBASE specific 
  481.                  (KenMayer).
  482. 1992-04-16  0.7  Minor corrections to make this 'Politically Correct'.
  483.                  (Ken Mayer (KenMayer))
  484. 1993-07-23  0.8  Changed text to fit 72 character right margin, and 
  485.                  added 'M->' to sample code.
  486.